<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/8
 * Time: 17:41
 */

namespace app\applet\model;


use think\Db;
use think\Exception;

class Invoice
{

  /**
   * 添加发票抬头
   */
  public function addTitle($member_id,$param){
    if(!in_array($param['invoice_type'],[1,2])){
        returnJson(-2003,"参数错误");
    }
    $param['member_id']=$member_id;
    $param['create_time']=time();
    $param['update_time']=time();

    $titleId=Db::name("invoice_title")->insertGetId($param);
    if($titleId<=0){
        returnJson(-2003,"添加失败");
    }
    return true;
  }
    /**
     * 删除发票抬头
     */
    public function removeTitle($param){



        $titleId=Db::name("invoice_title")->where($param)->delete();
        if($titleId<=0){
            returnJson(-2003,"删除失败");
        }
        return true;
    }

  /**
   * 获取抬头列表
   */
  public function titleList($member_id,$param,$current_page,$page_num){
      $where=[];
      $where['member_id']=$member_id;
      if(!empty($param['invoice_type'])){
          $where['invoice_type']=$param['invoice_type'];
      }
      $data = Db::name("invoice_title")
          ->field("title_id,credit_code,title_company")
          ->page($current_page,$page_num)
          ->where( $where)
          ->order("update_time desc")
          ->select();
      $total_num = Db::name("invoice_title")
          ->where( $where)
          ->count();
      $list['current_page'] = $current_page;
      $list['total_page'] = ceil($total_num / $page_num);
      $list['page_num'] = $page_num;
      $list['total_num'] = $total_num;
      $list['data'] = $data;
      return $list;
  }

    /**
     * @param $member_id
     * @param 添加发票
     */
  public function addInvoicer($member_id,$input){
      $order_list=explode(",",$input['order_list']);

      $num=Db::name("invoice_order")->where(['source_type'=>$input['invoice_source'],'order_id'=>['in',$order_list]])->count();
      if($num>0){
          returnJson(-2003,"有订单已经申请过发票");
      }
      $data=[];
      $data['member_id']=$member_id;
      $data['i_type']=$input['invoice_type'];
      $data['rise_type']=$input['title_type'];
      $data['con_type']=$input['invoice_content'];
      $data['invoice_source']=$input['invoice_source'];
      $data['invoice_email']=$input['invoice_email'];
      $data['create_time']=time();
      $data['update_time']=time();
      $data['status']=2;

      if($input['invoice_source']==1){

          $data['price']=Db::name("order")->where(['order_id'=>['in',$order_list]])->value("sum((order_score_money+order_coin_money+order_delivery_fee))");
      }else{
          $data['price']=1;
      }
      if( $input['invoice_type']==1){
          if($input['title_type']==1){//个人及政府单位
              $data['invoice_rise']=$input['invoice_title'];
          }elseif($input['title_type']==2){//企业
              $data['credit_code']=$input['credit_code'];
               $data['invoice_rise']=$input['invoice_title'];
          }
      }elseif( $input['invoice_type']==2){
              $data['invoice_rise']=$input['invoice_title'];
              $data['credit_code']=$input['credit_code'];
              $data['company_name']=$input['title_company'];
              $data['company_address']=$input['title_addr'];
              $data['reg_phone']=$input ['title_phone'];
              $data['bank_name']=$input['title_bank'];
              $data['bank']= $input['title_account'];
      }
      $invoiceOrder=[];
      foreach ($order_list as $item){
          $invoiceOrder[]=['order_id'=>$item,'source_type'=>$input['invoice_source'],'create_time'=>time(),'update_time'=>time()];
      }
      Db::startTrans();
      try {
          $num=Db::name("invoice")->insertGetId($data);

          if($num<=0){
              throw  new Exception("添加失败");
          }
          foreach ( $invoiceOrder as &$order){
              $order['invoice_id']=$num;
          }
          $num=Db::name("invoice_order")->insertAll($invoiceOrder);
          if($num<=0){
              throw  new Exception("添加失败");
          }
          Db::commit();
          return true;
      }catch (Exception $e){
          Db::rollback();
          returnJson(-2003,"添加失败");
      }


  }

    /**
     * 获取发票列表
     */
  public function invoiceList($where,$current_page,$page_num){

      $data = Db::name("invoice")
          ->field("invoice_rise,company_name,price,create_time,status,i_type,rise_type,con_type")
          ->where($where)
          ->page($current_page,$page_num)
          ->order("update_time desc")
          ->select();
      $total_num = Db::name("invoice")
          ->where($where)
          ->count();
      $list['current_page'] = $current_page;
      $list['total_page'] = ceil($total_num / $page_num);
      $list['page_num'] = $page_num;
      $list['total_num'] = $total_num;
      $list['data'] = $data;
      return $list;
  }

  /*
   * 消费商城
   */
  public function shopOrder($member_id){

      $orderList=Db::name("invoice_order")->where(['source_type'=>1])->column("order_id");
      $orderList=Db::name("order")->where(['member_id'=>$member_id,'status'=>['in',[12,5]],'order_id'=>['not in',  $orderList]])->field("order_id,store_id,source_type,update_time time,(order_score_money+order_coin_money+order_delivery_fee) total_money")->select();
      foreach ($orderList as &$order){
          if(in_array($order['source_type'],[2,3])){
              $order['company_name']="第三方商城";
          }elseif($order['source_type']==4){
              $order['company_name']="自营店铺";
          }else{
              $order['company_name']=Db::name("store_apply")->where(['sa_id'=>$order['store_id']])->value("company_name");
          }
          unset($order['store_id']);

      }

      return $orderList;
  }

  public function invoiceTitleInfo($input){
      $member_id=$input['member_id'];
      $title_id=$input['title_id'];
      return Db::name("invoice_title")->where(['title_id'=>$title_id,'member_id'=>$member_id])->find();

  }

}

